<?php
/**
 * RedCross Application
 *
 * @copyright Copyright (c) 2010, Kristian Stokke Tryggestad, Rune Vikestad, Andreas Flaten Wist, Simen Nicolai Echholt, Ole Henrik Jahren
 */

/**
 * Validation class for validating a password
 *
 * @category    RedCross
 * @package     RedCross_Validate
 */
class RedCross_Validate_Password extends Zend_Validate_Abstract
{
    /**
     * Validation failure message key for when the password does not contain upper case letters
     * @var string
     */
    const UPPER = 'upper';

    /**
     * Validation failure message key for when the password does not contain lower case letters
     * @var string
     */
    const LOWER = 'lower';

    /**
     * Validation failure message key for when the password does not contain digits
     * @var string
     */
    const DIGIT = 'digit';

    /**
     * Validation failure message template definitions
     * @var array
     */
    protected $_messageTemplates = array(
        self::UPPER     => "Passordet må inneholde minst en stor bokstav",
        self::LOWER     => "Passordet må inneholde minst en liten bokstav",
        self::DIGIT     => "Passordet må inneholde minst ett tall"
    );

    /**
     * Validator that validates the length of the password
     * @var Zend_Validate_StringLength
     */
    private $_lengthValidator;

    /**
     * Constructor
     *
     * Creates a new length validator and feeds it the minimum and
     * maximum length of the password
     * @param int $min
     * @param int $max
     * @return void
     */
    public function  __construct($min = 5, $max = null)
    {
        $this->_lengthValidator = new Zend_Validate_StringLength($min, $max);
        $this->_lengthValidator->setMessages(array(
            Zend_Validate_StringLength::TOO_SHORT => "Password must be at least %min% characters long",
            Zend_Validate_StringLength::TOO_LONG => "Password can be no longer than %max% characters"
        ));
    }

    /**
     * Returns the minimum length of the password
     * @return int
     */
    public function getMinLength()
    {
        return $this->_lengthValidator->getMin();
    }

    /**
     * Sets the minimum length of the password
     * @param int $min
     * @return RedCross_Validate_Password
     */
    public function setMinLength($min)
    {
        $this->_lengthValidator->setMin($min);
        return $this;
    }

    /**
     * Returns the maximum length of the password
     * @return int|null
     */
    public function getMaxLength()
    {
        return $this->_lengthValidator->getMax();
    }

    /**
     * Sets the maximum length of the password
     * @param int|null $max
     * @return RedCross_Validate_Password
     */
    public function setMaxLength($max)
    {
        $this->_lengthValidator->setMax($max);
        return $this;
    }

    /**
     * (non-PHPdoc)
     * @see library/Zend/Validate/Zend_Validate_Interface#isValid($value)
     */
    public function isValid($value)
    {
        $value = (string) $value;
        $this->_setValue($value);

        if (!$this->_lengthValidator->isValid($value)) {
            $this->_messages = array_merge($this->_messages, $this->_lengthValidator->getMessages());
        }

        if (!preg_match('/[A-Z]/', $value)) {
            $this->_error(self::UPPER);
        }

        if (!preg_match('/[a-z]/', $value)) {
            $this->_error(self::LOWER);
        }

        if (!preg_match('/\d/', $value)) {
            $this->_error(self::DIGIT);
        }

        return count($this->_messages) ? false : true;
    }

}